package com.c611.smartclassroom.controller;

import com.aliyuncs.exceptions.ClientException;
import com.c611.smartclassroom.ali.ali;
import com.c611.smartclassroom.model.SceneMode;
import com.c611.smartclassroom.service.SceneModeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sceneMode")
public class SceneModeController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SceneModeService sceneModeService;

    /**
     * 查询所有场景
     * @return
     */
    @RequestMapping("/queryAllSceneMode")
    public List<SceneMode> queryAllSceneMode(){

        return sceneModeService.queryAllSceneMode();
    }

    /**
     * 增加场景的方法
     * @param scene_id 场景编号
     * @param scene_name 场景名
     * @param number 第？个设备
     * @param deviceID 设备的编号
     * @param device_properties 设备的属性及值
     * @return
     */
    @PostMapping("/addSceneMode")
    public int addSceneMode(@Param("scene_id") Integer scene_id, @Param("scene_name") String scene_name, @Param("number") Integer number, @Param("deivceID") Integer deviceID, @Param("device_properties") String device_properties){
        if (scene_name == null) return 0;

        return sceneModeService.addSceneMode(scene_id, scene_name, number, deviceID, device_properties);
    }

    /**
     * 根据场景id删除场景
     * @param scene_id
     * @return
     */
    @PostMapping("/delSceneMode")
    public int delSceneMode(@Param("scene_id") Integer scene_id){
        if (scene_id == 0) return 0;
        return sceneModeService.delSceneMode(scene_id);
    }

    /**
     * 根据场景id查询场景
     * @param scene_id
     * @return
     */
    @PostMapping("/querySceneMode")
    public List<SceneMode> querySceneMode(@Param("scene_id") Integer scene_id){

        return sceneModeService.querySceneMode(scene_id);
    }

    /**
     * 根据场景id来控制各设备状态
     * @param scene_id
     * @throws ClientException
     */

    @PostMapping("/doSceneMode")
    public void doSceneMode(Integer scene_id) throws ClientException {

        //获取设备的deviceid和属性item
        List<SceneMode> scene = querySceneMode(scene_id);

        int deviceId1 = scene.get(0).getDeviceid1();
        String item1 = scene.get(0).getDeviceProperties1();

        int deviceId2 = scene.get(0).getDeviceid2();
        String item2 = scene.get(0).getDeviceProperties2();

        int deviceId3 = scene.get(0).getDeviceid3();
        String item3 = scene.get(0).getDeviceProperties3();

        int deviceId4 = scene.get(0).getDeviceid4();
        String item4 = scene.get(0).getDeviceProperties4();

        int deviceId5 = scene.get(0).getDeviceid5();
        String item5 = scene.get(0).getDeviceProperties5();

        int deviceId6 = scene.get(0).getDeviceid6();
        String item6 = scene.get(0).getDeviceProperties6();

        int deviceId7 = scene.get(0).getDeviceid7();
        String item7 = scene.get(0).getDeviceProperties7();

        int deviceId8 = scene.get(0).getDeviceid8();
        String item8 = scene.get(0).getDeviceProperties8();

        int deviceId9 = scene.get(0).getDeviceid9();
        String item9 = scene.get(0).getDeviceProperties9();

        int deviceId10 = scene.get(0).getDeviceid10();
        String item10 = scene.get(0).getDeviceProperties10();

        //获取设备的iotid
        DeviceMessageController deviceMessageController = new DeviceMessageController();
        String iotid1 = deviceMessageController.queryDeviceMessage(deviceId1).get(0).getIotid();
        String iotid2 = deviceMessageController.queryDeviceMessage(deviceId2).get(0).getIotid();
        String iotid3 = deviceMessageController.queryDeviceMessage(deviceId3).get(0).getIotid();
        String iotid4 = deviceMessageController.queryDeviceMessage(deviceId4).get(0).getIotid();
        String iotid5 = deviceMessageController.queryDeviceMessage(deviceId5).get(0).getIotid();
        String iotid6 = deviceMessageController.queryDeviceMessage(deviceId6).get(0).getIotid();
        String iotid7 = deviceMessageController.queryDeviceMessage(deviceId7).get(0).getIotid();
        String iotid8 = deviceMessageController.queryDeviceMessage(deviceId8).get(0).getIotid();
        String iotid9 = deviceMessageController.queryDeviceMessage(deviceId9).get(0).getIotid();
        String iotid10 = deviceMessageController.queryDeviceMessage(deviceId10).get(0).getIotid();

        //控制各设备的属性
        ali ali = new ali();
        ali.SetDeviceProperties(iotid1,item1);
        ali.SetDeviceProperties(iotid2,item2);
        ali.SetDeviceProperties(iotid3,item3);
        ali.SetDeviceProperties(iotid4,item4);
        ali.SetDeviceProperties(iotid5,item5);
        ali.SetDeviceProperties(iotid6,item6);
        ali.SetDeviceProperties(iotid7,item7);
        ali.SetDeviceProperties(iotid8,item8);
        ali.SetDeviceProperties(iotid9,item9);
        ali.SetDeviceProperties(iotid10,item10);

    }



}
